package com.example.myapplication.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.bean.IndexGridBean
import com.example.myapplication.databinding.IndexGridItemBinding

class IndexGridAdapter(private val data: ArrayList<IndexGridBean>) :
    RecyclerView.Adapter<IndexGridAdapter.ViewHolder>() {
    class ViewHolder(private var binding: IndexGridItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(itemBean: IndexGridBean, position: Int) {
            binding.textView.text = itemBean.title
            binding.imageView.setImageResource(itemBean.image)
            binding.root.setOnClickListener {
                Toast.makeText(
                    binding.root.context,
                    "你点击了第${position}个grid项",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = IndexGridItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(data[position], position)
    }
}